//praneet sharma
//2010061
//book referred - let us C by yashwant kanitkar

#include<stdio.h>
#include<string.h>
int pass_enter(char s[])
{
	
	
	FILE *fp ;
	char s1[80] ;
	
	fp = fopen ( "userpass.txt", "a" ) ;
	if ( fp == NULL )
	{
		puts ( "Cannot open file" ) ;
		exit(0) ;
	}
	printf ( "\nagain, enter username and password after a blank space(press enter twice to exit):\n" ) ;
	while ( strlen ( gets ( s1 ) ) > 0 )
	{
		fputs ( s1, fp ) ;
		fputs ( "\n", fp ) ;
	}
	fclose ( fp ) ;
}
		
int main()
{
	FILE *fp;
	char s[80] ;
	char temp[80];
	char p[80];

	printf("enter the username to check its availability:");
	gets(temp);

	
	fp = fopen ( "userpass.txt", "r" ) ;
	if ( fp == NULL )
	{
		puts ( "Cannot open file" ) ;
		exit(0) ;
	}
	int count=0;
	int count1=0;
	char ch=fgetc(fp);
	while(ch!=EOF)
	{
		int i=0,k=0;	
		while(ch!='\n')
		{
	
			s[i]=ch;
			i++;
		
			ch=fgetc(fp);
		}
	
	
		s[i]='\0';
		

		while(s[k]!=' ')
		{
			p[k]=s[k];
			
			k++;
			
		}
		p[k]='\0';
		if(strcmp(temp,p)==0)
		{
			printf("\nthe username entered by you is not available\n");
			count=1;
			break;
		
		}
		else
		{
			count=0;
			
		}
		count1++;
		ch=fgetc(fp);
	}
	if(count==0)
	{
		printf("\nthe username entered by you is valid\n");
		pass_enter(temp);
	}

}
